﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Node : MonoBehaviour, IComparable
{
    private float weight = 10;
    public Node Parent = null;
    public bool IsObstacle = false;

    public Node[] Neighbours = new Node[4];

    public MeshRenderer Render;

    private Color m_color;
    public Color Color {
        get => m_color;
        set {
            m_color = value;

            var mat2 = Instantiate(Render.material);
            mat2.color = value;
            Render.material = mat2;
        }
    }

    public float PassedCost {
        get => totalCost;
        set {
            totalCost = value;
            string v = value.ToString();
            if (v == "0")
            {
                v = "";
            }
            transform.GetComponentInChildren<Text>().text = v;

        }
    }

    public float Weight {
        get => weight;
        set {
            weight = value;
        }
    }

    private float totalCost = 0;


    private void Start()
    {
        Render = GetComponent<MeshRenderer>();
    }

    public Node(int w = 0)
    {
        this.Weight = w;
    }

    public Node[] GetNeighbour()
    {
        for (int i = 0; i < Neighbours.Length; i++)
        {
            if (Neighbours[i] == null || Neighbours[i].IsObstacle)
                continue;
            var mat2 = Instantiate(Render.material);
            //mat2.color = Color.blue;
            Neighbours[i].Render.material = mat2;
        }

        return Neighbours;
    }

    public void SetAsObstacle()
    {
        Color = Color.red;
        IsObstacle = true;
        transform.position = new Vector3(transform.position.x, 1, transform.position.z);
        transform.GetComponentInChildren<Text>().text = "";
    }

    public void SetAsPassable()
    {
        Color = Color.white;
        IsObstacle = false;
        transform.position = new Vector3(transform.position.x, 0, transform.position.z);
    }

    public void SetAsStart()
    {
        Color = Color.green;
        //transform.position += Vector3.up;
    }

    public void SetAsGoal()
    {
        Color = Color.yellow;
        //transform.position += Vector3.up;
    }


    public int CompareTo(object obj)
    {
        Node a = obj as Node;

        return this.Weight.CompareTo(a.Weight);
    }

    public void SetColor(Color color)
    {
        Color = color;
    }

    // 位置相等就是节点相等
    //public static bool operator == (Node a, Node b)
    //{
    //    if(a.transform.position == b.transform.position)
    //    {
    //        return true;
    //    }else
    //    {
    //        return false;
    //    }
    //}

    //public static bool operator !=(Node a, Node b)
    //{
    //    if (a.transform.position != b.transform.position)
    //    {
    //        return true;
    //    }
    //    else
    //    {
    //        return false;
    //    }
    //}

    //public override bool Equals(object other)
    //{
    //    return base.Equals(other);
    //}

    //public override int GetHashCode()
    //{
    //    return base.GetHashCode();
    //}
}

